<?php
/**
 * Slideshow Helper, responsible for managing the LINKS required for pagination.
 * ALL parameters are specified in the component.
 */
class SlideshowHelper extends Helper 
{
	var $helpers = array('Ajax','Html');
/**
 * Placeholder for the link style - defined in/by the component
 *
 * @var boolean
 * @access private
 */
	var $style = 'Html';
/**
 * Placeholder for the parameter style - defined in/by the component
 *
 * @var boolean
 * @access private
 */
	var $paramStyle = 'get';
/**
 * Placeholder for the pagination details
 *
 * @var array
 * @access private
 */
	var $_pageDetails = Array();
	
/**
* Displays a link to the previous page, where the page doesn't exist then
* display the $text
*
* @param string $text - text display: defaults to next
* @return string html for link/text for previous item
**/
	function prevPage($text='prev',$escapeTitle=true)
	{
		if (empty($this->_pageDetails)) { return false; }
		if ( !empty($this->_pageDetails['previousPage']) )
		{
            return $this->_generateLink($text,$this->_pageDetails['previousPage'],$escapeTitle);
		}
		return $text;
	}
	
/**
* Displays a link to the next page, where the page doesn't exist then
* display the $text
*
* @param string $text - text to display: defaults to next
* @return string html for link/text for next item
**/
	function nextPage($text='next',$escapeTitle=true)
	{
		if (empty($this->_pageDetails)) { return false; }
		if (!empty($this->_pageDetails['nextPage']))
		{
            return $this->_generateLink($text,$this->_pageDetails['nextPage'],$escapeTitle);
		}
		return $text;
	}

/**
* Displays a link to the first page
* display the $text
*
* @param string $text - text to display: defaults to next
* @return string html for link/text for next item
**/
	function firstPage($text='first',$escapeTitle=true)
	{
		if (empty($this->_pageDetails)) { return false; }
		if ($this->_pageDetails['page']<>1)
		{
        	return $this->_generateLink($text,1,$escapeTitle);
		}
		else
		{
			return false;
		}
	}

/**
* Displays a link to the last page
* display the $text
*
* @param string $text - text to display: defaults to next
* @return string html for link/text for next item
**/
	function lastPage($text='last',$escapeTitle=true)
	{
		if (empty($this->_pageDetails)) { return false; }
		if ($this->_pageDetails['page']<>$this->_pageDetails['pageCount'])
		{
	        return $this->_generateLink($text,$this->_pageDetails['pageCount'],$escapeTitle);
		}
		else
		{
			return false;
		}
	}


/**
* Internal method to generate links based upon passed parameters.
*
* @param string title for link
* @param string page the page number
* @param boolean escape title
* @param string the div to be updated by AJAX updates
* @return string html for link
**/
    function _generateLink ($title,$page=NULL,$escapeTitle,$AjaxDivUpdate="content") 
    {
		$getParams = $this->getParams; // Import any other pre-existing get parameters

		if ($this->_pageDetails['paramStyle']=="pretty")
		{
			$pageParams=$this->_pageDetails['importParams'];
		}
        $pageParams['show'] = $this->_pageDetails['show'];
        $pageParams['sortBy'] = $this->_pageDetails['sortBy'];
        $pageParams['direction'] = $this->_pageDetails['direction'];
        $pageParams['page'] = $page?$page:$this->_pageDetails['page'];
        if (isset($this->_pageDetails['sortByClass']))
        {
            $pageParams['sortByClass'] = $this->_pageDetails['sortByClass'];
        }    
        return $this->__generateLink($title,$escapeTitle,$getParams,$pageParams,$AjaxDivUpdate);
    }

/**
* Private/Internal method to generate links called by _generateLink only
*
* @param string title for link
* @param boolean escape title
* @param array the get parameters present for the original page request
* @param array the pagination parameters necessary to be included in the generate link
* @param string the div to be updated by AJAX updates
* @return string html for link
**/
    function __generateLink ($title="ErrorNeverBlank",$escapeTitle,$getParams=Array("error"=>"NeverEmpty"),$pageParams=Array("error"=>"NeverEmpty"),$AjaxDivUpdate="content") 
    {
		$getString = Array();
		$prettyString = Array();
		if ($this->_pageDetails['paramStyle']=="get")
		{
			$getParams = am($getParams,$pageParams);
		}
		else
		{
			foreach($pageParams as $key => $value)
			{
				if (isset($this->_pageDetails['Defaults'][$key]))
				{
					if (up($this->_pageDetails['Defaults'][$key])<>up($value))
					{
						$prettyString[] = "$key/$value";
					}
				}
				else
				{
					$prettyString[] = "$key/$value";
				}			
			}
		}
		foreach($getParams as $key => $value)
		{
			if ($this->_pageDetails['paramStyle']=="get")
			{
				if (isset($this->_pageDetails['Defaults'][$key]))
				{
					if (up($this->_pageDetails['Defaults'][$key])<>up($value))
					{
						$getString[] = "$key=$value";
					}
				}
				else
				{
					$getString[] = "$key=$value";
				}
			}
			else
			{
				$getString[] = "$key=$value";
			}			
		}
		$url = $this->url;
		if ($prettyString)
		{
			$prettyString = implode ("/", $prettyString);
			$url .= $prettyString;
		}
		if ($getString)
		{
			$getString = implode ("&", $getString);
			$url .= "?".$getString;
		}

		if (($this->style=="ajax")&&(isset($this->Ajax)))
		{
			return $this->Ajax->link(
							$title,
							$url,
								array(
									"update" => $AjaxDivUpdate
									),
							NULL,
							NULL,
							$escapeTitle
								);
		}
		else
		{
			return $this->Html->link(
							$title,
							$url,
							NULL,
							NULL,
							$escapeTitle
								);
		}
    }
}
?>
